﻿<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RequestDemo</title>
</head>
<body>
    <button id="btn">发起请求</button>

    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
    <script>
        //生成GUID
        (function () {
            if (window.guid)
                return;
            window.guid = function () {
                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                    return v.toString(16);
                });
            };
        })();

        //日期格式化
        (function () {

            if (Date.prototype.format) return;

            Date.prototype.format = function (fmt) { //author: meizz
                var o = {
                    "M+": this.getMonth() + 1, //月份
                    "d+": this.getDate(), //日
                    "h+": this.getHours(), //小时
                    "m+": this.getMinutes(), //分
                    "s+": this.getSeconds(), //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            };

        })();

        var config = {
            //应用Id
            appId: "AppAdmin",
            //应用密钥
            appSecret: "VjxNekN2G2z0qrjW"
        };
        //接口根地址
        var rootUrl = 'http://localhost:9599';

        $('#btn').click(function () {
            var businessParam = {
                userId: '123456789'
            };

            var busJsonBody = JSON.stringify(businessParam);

            $.ajax({
                //配置
                url: rootUrl+'/Demo/ApiSign',
                type: "POST",
                data: busJsonBody,
                dataType: 'json',//返回数据类型
                contentType:'application/json',//请求数据类型
                async: true,
                beforeSend: function (request) {
                    //签名算法:appId+time+guid+body+appSecret
                    var appId = config.appId;
                    var appSecret = config.appSecret;
                    var time = new Date().format('yyyy-MM-dd hh:mm:ss');
                    var newGuid = guid();
                    var sign = md5(appId + time + newGuid + busJsonBody + appSecret);

                    //设置header
                    request.setRequestHeader("appId", appId);
                    request.setRequestHeader("time", time);
                    request.setRequestHeader("guid", newGuid);
                    request.setRequestHeader("sign", sign);
                },
                success: function (resJson) {
                    if (resJson.Success)
                        alert('成功!');
                    else
                        alert('失败:' + resJson.Msg);
                }
            });
        });
    </script>
</body>
</html>